/* 
 * полезные функции
 */
function getById (id) {
	return document.getElementById (id);
}
function getByName (name) {
	return document.getElementsByName(name);
}
function getByClass (clazz) {
	return document.getElementsByClassName(clazz);
}
function getXmlHttp(){
	var xmlhttp;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
function ajax (url, method, config) {
    var paramsStr = '';
	var params = [];
    if (config.params) {
        for (var param in config.params) {
            params.push (param + "=" + encodeURIComponent(config.params[param]));
        }
        paramsStr = params.join("&");
    }
	
	var urlWithParams = url;
	var requestBody = null;
    if (paramsStr) {
        if (method == "GET") {
            urlWithParams += "?" + paramsStr;
        }
        else {
            requestBody = paramsStr;
        }
    }
	
	var xmlhttp = getXmlHttp();
	xmlhttp.open(method, urlWithParams, true);
    if (method == "POST") {
        xmlhttp.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
    }
	xmlhttp.onreadystatechange = (function () {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				config.success (JSON.parse(xmlhttp.responseText));
			}
		}
	});
	xmlhttp.send(requestBody);
}